package Demo1;

//最优情况比较次数为log2n（完全二叉树）
//最坏为n/2（单分支树）
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root = null;

    public boolean search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                cur = cur.right;
            } else if (cur.val > val) {
                cur = cur.left;
            } else {
                return true;
            }
        }
        return false;
    }
    //插入的位置一定是叶子
    //不能插入相同的数据的
    public void insert(int val) {
        TreeNode node = new TreeNode(val);
        //注意判断根空不空，防止空指针异常
        if(root == null){
            root = node;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else{
                throw new valError("输入的val在二叉搜索树中已经存在");
            }
        }
        if(parent.val < val){
            parent.right = node;
        }else{
            parent.left = node;
        }
    }
    public void remove(int val){
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else{//说明找到了要删除的节点
                removeNode(parent,cur);
                return;
            }
        }
    }
    private void removeNode(TreeNode parent,TreeNode cur){
        if(cur.left == null){//被删节点的左边为空
            if(cur == root){
                root =cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        } else if (cur.right == null) {//被删节点的右边为空
            if(cur == root){
                root =cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{//被删节点的左右两边都不为空
            //可以去找左子树的最右边的元素，也可以找右子树最左边的元素,
            // 对cur进行替换，
            // 然后删除掉右子树最左边或者左子树最右边的元素
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            //右子树最左边说明左边没有元素了
            if(target == targetParent.left){
                targetParent.left = target.right;
            }else{//没进入循环的情况
                targetParent.right = target.right;
            }
        }
    }
}